package com.shuoyue.ycgk.ui.mine.info;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;

import butterknife.BindView;
import butterknife.OnClick;

public class UpdateNickActivity extends BaseMvpActivity<UpdateNickContract.Presenter>
        implements UpdateNickContract.View {

    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.l_right)
    FrameLayout lRight;
    @BindView(R.id.tv_nickname)
    EditText tvNickname;
    @BindView(R.id.clear)
    ImageView clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_update_nick;
    }

    @Override
    protected void initView() {
        pageTitle.setText("修改昵称");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        tvRight.setTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));

    }

    @Override
    public void initData() {
        super.initData();
        mPresenter = new UpdateNickContract.Presenter();
        mPresenter.attachView(this);
        UserInfo userInfo = SPUtils.getUserInfo(mContext);
        tvNickname.setText(userInfo.getUsername());
        tvNickname.setSelection(tvNickname.getText().length());

    }

    @OnClick({R.id.tv_right, R.id.clear, R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                updateNick();
                break;
            case R.id.clear:
                tvNickname.setText("");
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    void updateNick() {
        if (tvNickname.getText().toString().length() == 0) {
            toast("请输入昵称");
            return;
        }
        mPresenter.updateUserInfo(tvNickname.getText().toString(), null);
    }

    @Override
    public void sucess(String nickName, String head) {
        UserInfo userInfo = SPUtils.getUserInfo(mContext);
        userInfo.setUsername(nickName);
        SPUtils.updateUserInfo(mContext, userInfo);
        toast("修改成功");
        finish();
    }
}
